<?php
if (!defined('sugarEntry')) {
    session_start();
    session_destroy();
    header('Location: ../iphone.php');
}
/*********************************************************************************
* SugarCRM is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, 
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3, 
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo. If the display of the logo is not reasonably feasible for
* technical reasons, the Appropriate Legal Notices must display the words
* "Powered by SugarCRM".
********************************************************************************/
/*********************************************************************************
* Function:    sugar-iphone-client for SugarCRM
* Description: Include file to list related information to module records
* Author:      Editha Kuske - 2008, Richard John Jensen 2009
* Revision:      $Id: show_related.php 6 2009-12-26 16:59:05Z richardjohnjensen $
********************************************************************************/

require_once('sugar-iphone-client/detail_masks.php');

if (isset($_GET['main_bean'])) {
    $_SESSION['main_bean']['bean'] = $_GET['main_bean'];
    $_SESSION['main_bean']['bean_id'] = $_GET['main_bean_id'];
    $_SESSION['main_bean']['bean_name'] = $_GET['main_bean_name'];
}

$main_bean = $_SESSION['main_bean']['bean'];
$main_bean_id = $_SESSION['main_bean']['bean_id'];
$main_bean_name = $_SESSION['main_bean']['bean_name'];

$module_name = $ar_beans[$main_bean]['module_name'];
$related_module_name = $ar_beans[$list_bean]['module_name'];
$action = get_par('action');

// --- Headerline --------------------
if (isset($_POST['btn_create'])) {
    $id = 0;
    $sugar_soap_client->getMetaData($ar_beans[$list_bean]);
    if (isset($ar_meta_fields['account_id'])) {
        $ar_meta_fields['account_id']['value'] = $main_bean_id;
    }
    if (execute_submit($id, $ar_beans[$list_bean])) {
        $action = '';
        
        switch ($list_bean) {
			case 'Contact':
				//        case 'Call':
				//        case 'Meeting':
				$sugar_soap_client->set_relationship($module_name, $main_bean_id, $related_module_name, $id);
				break;
			default;
				break;
        }
        
        if ($id) {
            header("Location: iphone.php?bean=$list_bean&list_bean=$list_bean&modus=detail&bean_id=$id");
        }
    }
}

header_line($main_bean, 'related');
$html = <<<__BLOCK__
<tr><td colspan="2">
<input type="hidden" name = "bean" value="$bean" />
<input type="hidden" name = "modus" value="$modus" />
<input type="hidden" name = "action" value="$action" />
</td></tr>
__BLOCK__;
echo $html;

switch ($action) {
    
	case "new":
		$html = <<<__BLOCK__
		<tr class="tr-odd"><td>$main_bean</td><td>$main_bean_name</td></tr>
		<tr class="tr-even"><td colspan="2"><br />Create a related $list_bean:</td></tr>

__BLOCK__;
		echo $html;
		
		if (!isset($_POST['btn_create'])) {
			$sugar_soap_client->getMetaData($ar_beans[$list_bean]);
		}
		
		$module_name = $ar_beans[$list_bean]['module_name'];
		
		if (isset($ar_meta_fields['parent_type'])) {
			$ar_meta_fields['parent_type']['value'] = $ar_beans[$main_bean]['module_name'];
			$ar_meta_fields['parent_type']['required'] = 1;
			$ar_meta_fields['parent_id']['value'] = $main_bean_id;
			$ar_meta_fields['parent_id']['required'] = 1;
		} else {
			$ar_meta_fields['account_id']['value'] = $main_bean_id;
			$ar_meta_fields['account_id']['required'] = 1;
			$ar_meta_fields['account_id']['hidden'] = 1;
		}
		
		create_record($ar_beans[$list_bean]);
		break;
    
	case "show":
		$html = <<<__BLOCK__
		<tr class="tr-odd"><td colspan="2">$main_bean: $main_bean_name</td></tr>
		<tr class="tr-even"><td colspan="2"></td></tr>
		<tr class="tr-even"><td colspan="2">Related $related_module_name:</td></tr>

__BLOCK__;
		echo $html;
		
		//EK projects start
		if ($list_bean == 'ProjectTask') {
			//       $ar_bean = $sugar_soap_client->get_project_tasks($main_bean_id);
			$ar_bean = $sugar_soap_client->get_list($ar_project_tasks_descr, "All", "(project_id='$main_bean_id')", 0, 100);
		} else {
			$ar_bean = $sugar_soap_client->get_related($module_name, $main_bean_id, $related_module_name);
		}
		//EK projects end
		if (empty($ar_bean)) {
			$html = <<<__BLOCK__
			<tr><td colspan="2">none</td></tr>
__BLOCK__;
			echo $html;
		} else {
			$data_list_par = "bean=$list_bean&amp;list_bean=$list_bean&amp;modus=detail";
			include("sugar-iphone-client/show_data_list.php");
		}
		break;
    
	case "set_ok":
		$sugar_soap_client->set_relationship($module_name, $main_bean_id, $related_module_name, $_GET['bean_id']);
		// and go ahead
	default:
		$html = <<<__BLOCK__
		<tr class="tr-even"><td colspan="2"><hr /></td></tr>
		<tr class="tr-odd"><td>$main_bean</td><td>$main_bean_name</td></tr>
		<tr class="tr-even"><td colspan="2">Show related</td></tr>
		<tr class="tr-even"><td colspan="2">
__BLOCK__;
		echo $html;

		//EK projects start
		if ($main_bean == 'Project') {
			$html = <<<__BLOCK__
			<a href="iphone.php?bean=$main_bean&amp;action=show&amp;list_bean=ProjectTask&amp;modus=related">ProjectTasks</a></td></tr>
__BLOCK__;
		echo $html;
		} else {
			//EK projects end
			$html = <<<__BLOCK__
			<a href="iphone.php?bean=$main_bean&amp;action=show&amp;list_bean=Opportunities&amp;modus=related">Opportunities</a></td></tr>
			<tr class="tr-even"><td colspan="2">
			<a href="iphone.php?bean=$main_bean&amp;action=show&amp;list_bean=Contacts&amp;modus=related">Contacts</a></td></tr>
			<a href="iphone.php?bean=$main_bean&amp;action=show&amp;list_bean=Calls&amp;modus=related">Calls</a></td></tr>
			<tr class="tr-even"><td colspan="2">
			<tr class="tr-even"><td colspan="2">
			<a href="iphone.php?bean=$main_bean&amp;action=show&amp;list_bean=Meetings&amp;modus=related">Meetings</a></td></tr>
__BLOCK__;
			echo $html;

		}
		// --------- Create related ----------------------
		//EK projects start
		if ($main_bean != 'Project') {
			//EK projects end
			$html = <<<__BLOCK__
			<tr class="tr-even"><td colspan="2"><hr /></td></tr>
			<tr class="tr-even"><td colspan="2">Create related</td></tr>
			<a href="iphone.php?bean=$main_bean&amp;action=new&amp;list_bean=Opportunities&amp;modus=related">Opportunity</a></td></tr>
			<tr class="tr-even"><td colspan="2">
			<tr class="tr-even"><td colspan="2">
			<a href="iphone.php?bean=$main_bean&amp;action=new&amp;list_bean=Contacts&amp;modus=related">Contact</a></td></tr>
			<tr class="tr-even"><td colspan="2">
			<tr class="tr-even"><td colspan="2">
			<a href="iphone.php?bean=$main_bean&amp;action=new&amp;list_bean=Meetings&amp;modus=related">Meeting</a></td></tr>
			<a href="iphone.php?bean=$main_bean&amp;action=new&amp;list_bean=Calls&amp;modus=related">Call</a></td></tr>
__BLOCK__;
			echo $html;
			//EK projects start
		}
		//EK projects end
		break;
}

?>